// $.get("header_menu.html",function(data){ $("#header").html(data); });
$.get("footer.html",function(data){ $("#footer").html(data); });

$(document).ready(function() {
    // $("#account").addClass("active");
    // $("#account1").addClass("active");

    getData1(1);

    function pageDivd(curr, page_num) {
        layui.use(['laypage', 'layer'], function () {
            var laypage = layui.laypage
                , layer = layui.layer;
            laypage.render({
                elem: 'demo7',
                count: page_num,
                limit: 5,
                curr: curr,
                jump: function (obj, first) {
                    if (!first) {
                        getData1(obj.curr);

                    }
                }
            });
        })
    }

    function getData1(curr) {
        $.ajax({
            type: "get",
            url: "http://114.116.72.220:9999/AccountMange/",
            async: true,
            data: {
                page: curr,
                row: 5,
                keyword: $("#search").val()
            },
            success: function (data) {
                console.log(data);
                if (data[0].length == 0) {
                    layer.msg('暂无数据');
                }
                TableView(data[0]);
                pageDivd(curr, data[1]);
            }
        });
    }

    function TableView(data) {
        $('#account_table').html('');

        for (var i = 0; i < data.length; i++) {
            var delText;
            if (data[i].user_role != '高级管理员') {
                delText = '<img src="img/delete.png" class="delete"/>';
            } else {
                delText = '无权限'
            }

            var trText = `<tr class="${i % 2 ? '' : 'tr_back'}">
		      		    
		      		    		<td>${i + 1}</td>
		      		    		<td>${data[i].user_no}</td>
		      		    		<td>
		      		    			${data[i].user_name}
		      		    		</td>
		      		    		<td>
		      		    			${data[i].user_role}
		      		    		</td>
		      		    		<td>
		      		    			${data[i].time}
		      		    		</td>
		      		    		<td index="${data[i].user_id}">
		      		    			${delText}
		      		    		</td>
                        </tr>
		      		    	`;
            $('#account_table').append(trText);
        }

        $('.delete').click(function () {

            $.ajax({
                type: "get",
                url: "http://114.116.72.220:9999/DeleteUser",
                async: true,
                data: {
                    userId: $(this).parent().attr('index')
                },
                success: function (data) {
                    if (data == 'success') {
                        layer.msg('删除成功', {time: 500}, function () {
                            location.reload();
                        })
                    }
                }
            });
        });

    }

    //添加管理员
    $('#new_add').click(function () {
        var Index;
        console.log($.cookie('UserRight'));
        var Text = $('#auth').val();
        switch (Text) {
            case '高级管理员':
                Index = 1;
                break;
            case '普通管理员':
                Index = 2;
                break;
            case '商品管理员':
                Index = 3;
                break;
            case '订单管理员':
                Index = 4;
                break;

        }

        if ($('#userId').val() == '') {
            layer.msg('登录账号不能为空')

        } else if ($('#userName').val() == '') {
            layer.msg('登录名称不能为空')

        } else if ($('#password').val() == '') {
            layer.msg('登录密码不能为空')
        } else {

            if ($.cookie('UserRight') == 1 || $.cookie('UserRight') == '10086') {
                $.ajax({
                    type: "get",
                    url: "http://114.116.72.220:9999/Register",
                    async: true,
                    data: {
                        userNo: $('#userId').val(),
                        userName: $('#userName').val(),
                        password: $('#password').val(),
                        role: Index,
                        userId: $.cookie('UserId')
                    },
                    success: function (data) {
                        if (data[0] == 'failure') {
                            if (data[1] == 'No') {
                                // console.log(1)
                                layer.msg('登录账号重复')
                            } else if (data[1] == 'Name') {
                                layer.msg('登录名重复')
                            } else if (data[1] == 'error') {
                                layer.msg('新增异常请重试')
                            }
                        } else {
                            layer.msg('新增成功', {time: 500}, function () {
                                location.reload();
                            })
                        }
                    }
                });
            } else {
                layer.msg('无权限');
            }

        }
    });

    $('.check1').click(function () {
        getData1(1);
    });

});